//
// Created by Jisam on 2024/7/15.
//
#include <iostream>
#include <queue>
using namespace std;
int minOperations (int n, int k) {

    priority_queue<int> pq;
    pq.push (n);
    int operations = 0;
    while (!pq.empty ()) {
        int u = pq.top ();
        pq.pop ();
        if (u == 1) {
            continue;
        }
        if (u <= k) {
            pq.push (1);
        } else {
            pq.push (u - (k - 1));
            for (int i = 0; i < k - 1; i++) {
                pq.push (1);
            }
        }
        operations++;
    }
    return operations;
}
int main () {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--) {
        int n, k;
        cin >> n >> k;
        cout << minOperations (n, k) << endl;
    }
    return 0;
}